namespace SimpleWebServices.Configuration
{
    using System.Configuration;
    using System.Xml.XPath;
    using SimpleWebServices.Web;
    using Xunit;

    public sealed class AnalyticsSettingsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<AnalyticsSettings>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("analytics")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            AnalyticsSettings actual = (
                "<analytics enabled='true'>" +
                    "<google tracker='1234' />" +
                "</analytics>").Deserialize<AnalyticsSettings>();

            Assert.True(actual.Enabled);
            Assert.Equal<string>("1234", actual.Google.Tracker);
        }

        [Fact]
        public void deserializeEmpty()
        {
            AnalyticsSettings actual = "<analytics />".Deserialize<AnalyticsSettings>();

            Assert.False(actual.Enabled);
        }

        [Fact]
        public void serialize()
        {
            AnalyticsSettings obj = new AnalyticsSettings
            {
                Enabled = true,
                Google = new GoogleAnalytics()
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/analytics[@enabled='true']/google)"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new AnalyticsSettings());
        }

        [Fact]
        public void prop_Enabled()
        {
            Assert.True(new PropertyExpectations(typeof(AnalyticsSettings).GetProperty("Enabled"))
                .XmlAttribute("enabled")
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Google()
        {
            Assert.True(new PropertyExpectations(typeof(AnalyticsSettings).GetProperty("Google"))
                .XmlElement("google")
                .IsAutoProperty<GoogleAnalytics>()
                .Result);
        }

        [Fact]
        public void op_Render_whenEnabled()
        {
            AnalyticsSettings obj = new AnalyticsSettings
            {
                Enabled = true
            };

            Assert.Equal<string>("<div id='analytics' class='enabled'></div>", obj.Render());
        }

        [Fact]
        public void op_Render_whenDisabled()
        {
            AnalyticsSettings obj = new AnalyticsSettings
            {
                Enabled = false
            };

            Assert.Equal<string>("<div id='analytics' class='disabled'></div>", obj.Render());
        }
    }
}